﻿using Net_SCADA.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Net_SCADA
{
    public partial class Form_Menu : fdBaseForm
    {

        string[] fontName = new string[] { "Microsoft YaHei", "Arial" };

        string[][] s = new string[2][] {new string[2] { "用户注销", "User Logout" },new string[2] { "用户登录", "User Login" } };

        public List<Button> MenuButton=new List<Button>();
        public Form_Menu()
        {
            InitializeComponent();
        }

        public void Form_Menu_Load(object sender , EventArgs e)
        {
            LoadMenuButtons();
            this.Location = new Point(netForms.fmTop.Width - this.Width ,netForms.fmTop.Height);
        }

        private void LoadMenuButtons()
        {
            int w =netForms.fmTop.btnWidth;
            int h=netForms.fmTop.btnHeight;
            this.Size =new Size( w + 4,(h+2)* netForms.MenuBtn.Count+2);
            Size size = new Size(w, h);//上下两个像素
            int i=0;
            foreach (int item in netForms.MenuBtn.Keys)
            {
                Point p = new Point(2, (h+2)*i+2);
                MenuButton.Add(new Button()
                {
                    Location = p ,
                    Size = size,
                    Tag = item ,
                    Text = netForms.MenuBtn[item][MultipleLanguage.systemLanguageIndex] ,
                    Font = fdPublicFunction.Getfont(size.Height / 3 , fontName[MultipleLanguage.systemLanguageIndex], FontStyle.Regular)
                });
                this.Controls.Add(MenuButton.Last());
                MenuButton.Last().Click += Btn_Click;
                i++;
            }
                MenuButton[7].Text = fdUsers.UserRights > 0 ? s[0][formLanguageIndex] : s[1][formLanguageIndex];
        }

        private void Btn_Click(object sender , EventArgs e)
        {
            // "参数总览,工艺设计,执行工艺,系统设定,警报显示,设备监视,历史记录,用户登陆,退出控制";
            Button btn = (Button)sender;//获取被点击的控件,按钮  
            this.Hide();
            if (netForms.Formindex == (int)btn.Tag) return;
            switch ((int)btn.Tag)
            {
                case 0://参数总览
                    netForms.Formindex = 0;
                    break;
                case 1://工艺设计
                    if (fdUsers.UserRights == 4) { netForms.Formindex = 1; } else { netForms.showLogin(1); }
                    break;
                case 2://执行工艺
                    if (fdUsers.UserRights > 0) { netForms.Formindex = 2;netForms.fmRun.ShowCurrentPara(); } else { netForms.showLogin(2); }//执行工艺界面
                    break;
                case 3://警报显示
                    netForms.Formindex = 3;
                    NetLine.isNeedFlashAlarm = true;
                    break;
                case 4://系统设定
                    if (fdUsers.UserRights == 4) { netForms.Formindex = 4; } else { netForms.showLogin(4); }
                    break;
                case 6://历史记录
                    fdPublicFunction.Query();
                    break;
                case 7://用户登陆
                    btnLogin();
                    break;
                case 8://退出控制
                    if (fdUsers.UserRights > 0)
                    {//退出界面
                        netForms.showFormExit();
                    }
                    else
                    {//登陆界面
                        netForms.showLogin(8);
                    }
                    break;
                case 5://阀值设置
                    if (fdUsers.UserRights == 4) { netForms.Formindex = 5; } else { netForms.showLogin(5); }
                    break;
                case 9://切换语言
                    //btnLanguageExchange();
                    break;
                default:
                    break;
            }
        }
        private void btnLogin()
        {
            if (fdUsers.UserRights > 0)
            {
                fdUsers.UserLogout();
                //netForms.fmMenu.MenuButton[6].Text = "用户登录";
                netForms.Formindex = 0;
            }
            else
            {
                netForms.showLogin(0);
            }
        }

        private void Form_Menu_Deactivate(object sender , EventArgs e)
        {
            Dispose();
        }

        private void Form_Menu_Activated(object sender, EventArgs e)
        {
            //if(formLanguageIndex!=MultipleLanguage.systemLanguageIndex)
            //{
            //    formLanguageIndex = MultipleLanguage.systemLanguageIndex;
            //    for (int i = 0; i < MenuButton.Count; i++)
            //    {
            //        if (MenuButton[i].Tag != null)
            //        {
            //            int v = (int)MenuButton[i].Tag;
            //            MenuButton[i].Text = netForms.MenuBtn[v][formLanguageIndex];
            //        }
            //    }
            //    MenuButton[7].Text = fdUsers.UserRights > 0 ? s[0][formLanguageIndex] : s[1][formLanguageIndex];
            //}
        }
    }
}
